package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.allen.library.SuperButton;
import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.MiBaoResetBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.view.SelectMenuTipsDilaog;
import com.yiwei.jmx.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3XueXinMiBaoResetActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.btn_wechat)
    ImageView btnWechat;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_tips)
    SuperButton txtTips;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.btn_pass_eye)
    ImageView btnPassEye;
    @BindView(R.id.edit_repass)
    EditText editRepass;
    @BindView(R.id.btn_repass_eye)
    ImageView btnRepassEye;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Unbinder unbinder;
    private String verifycode;
    private String account;
    private SelectMenuTipsDilaog selectMenuTipsDilaog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xuexin_question_reset_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        selectMenuTipsDilaog = new SelectMenuTipsDilaog(this);
        verifycode = getIntent().getStringExtra("verifycode");
        account = getIntent().getStringExtra("account");
        if (TextUtils.isEmpty(account)) {
            account = "";
        }
        txtTips.setText("恭喜您，密保验证安全通过，按以下要求重置密码登录用户名" + account);
    }

    @OnTextChanged({R.id.edit_pass, R.id.edit_repass})
    void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(UI.toString(editPass)) && !TextUtils.isEmpty(UI.toString(editRepass))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }

    public void getMiBaoReset(String password, String verifycode) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        MiBaoResetBean bean = new MiBaoResetBean();
        bean.password = password;
        bean.verifycode = verifycode;
        Api.api_service.getMiBaoReset(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(model -> {
                    selectMenuTipsDilaog.setOnChooseListener(new SelectMenuTipsDilaog.OnChooseListener() {
                        @Override
                        public void onLeft() {
                            finish();
                        }

                        @Override
                        public void onRight() {
                            selectMenuTipsDilaog.dismiss();
                        }
                    }).showOneSelectData("恭喜您", R.mipmap.dialog_tips_success, "重置密码成功，赶紧去登录学信网认证吧", "登录学信网").show();


                });
    }

    private boolean codeViewInputCheck() {
        if (TextUtils.isEmpty(UI.toString(editPass))) {
            ToastUtil.makeText(getApplicationContext(), "新密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(UI.toString(editRepass))) {
            ToastUtil.makeText(getApplicationContext(), "重复新密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    private void showBackDialog() {
        selectMenuTipsDilaog.setOnChooseListener(new SelectMenuTipsDilaog.OnChooseListener() {
            @Override
            public void onLeft() {
                finish();
            }

            @Override
            public void onRight() {
                selectMenuTipsDilaog.dismiss();
            }
        }).showTwoSelectData("恭喜您", R.mipmap.img_idcard_fail_dialog, "只有找回密码，才能完成学信网认证", "我要离开", "继续找回").show();
    }

    @Override
    public void onBackPressed() {
        showBackDialog();
    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                showBackDialog();
                break;
            case R.id.btn_submit:
                if (codeViewInputCheck()) {
                    getMiBaoReset(UI.toString(editPass), verifycode);
                }
                break;
        }
    }
}
